package song.NO_13;

import java.util.HashMap;

public class Solution {
    private String str = "";
    private int result = 0;
    private final static HashMap<String, Integer> mapping = new HashMap<>(){
        {
            put("I", 1);
            put("V", 5);
            put("X", 10);
            put("L", 50);
            put("C", 100);
            put("D", 500);
            put("M", 1000);
        }
    };
    public static int group_calc(String f, String l){
        StringBuilder str1 = new StringBuilder();
        str1.append(f).append(l);
        switch(str1.toString()){
            case "IV": return 4;
            case "IX": return 9;
            case "XL": return 40;
            case "XC": return 90;
            case "CD": return 400;
            case "CM": return 900;
//            default: return -1;
        }
        return -1;
    }
    public Solution(){}
    public Solution(final String str){
        this.str = str;
    }
    public void exchanging(){
        for (int i = 0; i != this.str.length(); ++i){
            if (i != this.str.length() - 1){
                if (Solution.group_calc(Character.toString(this.str.charAt(i)),
                        Character.toString(this.str.charAt(i+1))) == -1)
                    this.result += Solution.mapping.get(Character.toString(this.str.charAt(i)));
                else {
                    StringBuilder str_group = new StringBuilder();
                    str_group.append(this.str.charAt(i)).append(this.str.charAt(i+1));
                    this.result += Solution.group_calc(Character.toString(this.str.charAt(i)),
                            Character.toString(this.str.charAt(i+1)));
                    ++i;

                }
            }
            else
                this.result += Solution.mapping.get(Character.toString(this.str.charAt(i)));
        }
    }
    public int result_return(){return this.result;}
}
